#!/bin/bash

echo "正在自动修改 projectsimporter 支持 VS2005-2022..."

# 备份原文件
cp ./include/globals.h ./include/globals.h.backup
cp ./plugins/projectsimporter/projectsimporter.cpp ./plugins/projectsimporter/projectsimporter.cpp.backup

# 1. 修改 globals.h - 添加新的文件类型枚举
sed -i '/ftMSVC10Project,/a\    ftMSVC11Project,  // VS2012\
    ftMSVC12Project,  // VS2013\
    ftMSVC14Project,  // VS2015\
    ftMSVC15Project,  // VS2017\
    ftMSVC16Project,  // VS2019\
    ftMSVC17Project,  // VS2022' ./include/globals.h

sed -i '/ftMSVC7Workspace,/a\    ftMSVC10Workspace, // VS2010+ Workspace' ./include/globals.h

# 2. 修改 projectsimporter.cpp - 添加包含
sed -i '/#include "msvc10loader.h"/a\#include "msvc14loader.h"  // VS2015' ./plugins/projectsimporter/projectsimporter.cpp

# 3. 修改 CanHandleFile 方法
sed -i '/ftMSVC10Project/ {a\            || ft == ftMSVC11Project\
            || ft == ftMSVC12Project\
            || ft == ftMSVC14Project\
            || ft == ftMSVC15Project\
            || ft == ftMSVC16Project\
            || ft == ftMSVC17Project\
            || ft == ftMSVC10Workspace
}' ./plugins/projectsimporter/projectsimporter.cpp

# 4. 修改 OpenFile 方法
sed -i '/case ftMSVC10Project:/ {a\        case ftMSVC11Project:    \
        case ftMSVC12Project:    \
        case ftMSVC14Project:    \
        case ftMSVC15Project:    \
        case ftMSVC16Project:    \
        case ftMSVC17Project:    \
}' ./plugins/projectsimporter/projectsimporter.cpp

sed -i '/case ftMSVC7Workspace:/a\        case ftMSVC10Workspace:  ' ./plugins/projectsimporter/projectsimporter.cpp

# 5. 修改 LoadProject 方法
sed -i '/case ftMSVC10Project:/ {a\            case ftMSVC11Project:\
                loader = new MSVC11Loader(prj); break;\
            case ftMSVC12Project:\
                loader = new MSVC12Loader(prj); break;\
            case ftMSVC14Project:\
                loader = new MSVC14Loader(prj); break;\
            case ftMSVC15Project:\
                loader = new MSVC15Loader(prj); break;\
            case ftMSVC16Project:\
                loader = new MSVC16Loader(prj); break;\
            case ftMSVC17Project:\
                loader = new MSVC17Loader(prj); break;\
}' ./plugins/projectsimporter/projectsimporter.cpp

# 6. 修改 LoadWorkspace 方法
sed -i '/case ftMSVC7Workspace:/a\            case ftMSVC10Workspace:\
                pWsp = new MSVC10WorkspaceLoader; break;' ./plugins/projectsimporter/projectsimporter.cpp

echo "修改完成！已创建备份文件：*.backup"
echo "现在需要创建 msvc14loader.h 和 msvc14loader.cpp 文件"
